package com.hbase;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class HbaseWork {
    Configuration conf = null;
    Connection conn = null;
    HBaseAdmin admin = null;

    @Before
    public void init() throws IOException {
        //获取一个配置文件对象
        conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "mylinux01,mylinux02");
        conf.set("hbase.zookeeper.property.clientPort", "2181");
        //通过conf获取到hbase集群的连接
        conn = ConnectionFactory.createConnection(conf);
    }

    //创建一张hbase表
    @Test
    public void createTable() throws IOException {
        //获取HbaseAdmin对象用来创建表
        admin = (HBaseAdmin) conn.getAdmin();
        //创建Htabledesc描述器，表描述器
        final HTableDescriptor user = new HTableDescriptor(TableName.valueOf("user"));
        //指定列族
        user.addFamily(new HColumnDescriptor("friend"));
        admin.createTable(user);
        System.out.println("user表创建成功！！");
    }

    //插入一条数据
    @Test
    public void putData() throws IOException {
        //需要获取一个table对象
        final Table user = conn.getTable(TableName.valueOf("user"));
        //准备put对象
        final Put put = new Put(Bytes.toBytes("uid1"));//指定rowkey
        put.addColumn(Bytes.toBytes("friend"), Bytes.toBytes("uid2"), Bytes.toBytes("uid2"));
        put.addColumn(Bytes.toBytes("friend"), Bytes.toBytes("uid3"), Bytes.toBytes("uid3"));
        put.addColumn(Bytes.toBytes("friend"), Bytes.toBytes("uid4"), Bytes.toBytes("uid4"));

        final Put put2 = new Put(Bytes.toBytes("uid2"));//指定rowkey
        put2.addColumn(Bytes.toBytes("friend"), Bytes.toBytes("uid1"), Bytes.toBytes("uid1"));
        put2.addColumn(Bytes.toBytes("friend"), Bytes.toBytes("uid3"), Bytes.toBytes("uid3"));

        final Put put3 = new Put(Bytes.toBytes("uid3"));//指定rowkey
        put3.addColumn(Bytes.toBytes("friend"), Bytes.toBytes("uid1"), Bytes.toBytes("uid1"));
        put3.addColumn(Bytes.toBytes("friend"), Bytes.toBytes("uid2"), Bytes.toBytes("uid2"));

        final Put put4 = new Put(Bytes.toBytes("uid4"));//指定rowkey
        put4.addColumn(Bytes.toBytes("friend"), Bytes.toBytes("uid1"), Bytes.toBytes("uid1"));

        List<Put> puts = new ArrayList<>();
        puts.add(put);
        puts.add(put2);
        puts.add(put3);
        puts.add(put4);

        user.put(puts);
        //关闭table对象
        user.close();
        System.out.println("插入数据到user表成功！！");
    }

    @Test
    public void deleteData() throws IOException {
        String uid = "uid2";
        System.out.println("准备删除" + uid);
        //记录删除操作
        List<Delete> deletes = new ArrayList<>();

        final Table user = conn.getTable(TableName.valueOf("user"));

        final Delete delete = new Delete(Bytes.toBytes(uid));
        deletes.add(delete);
        //准备get对象
        final Get get = new Get(Bytes.toBytes(uid));
        //指定查询friend 找到uid2的所有朋友
        get.addFamily(Bytes.toBytes("friend"));
        //执行查询
        final Result result = user.get(get);
        final Cell[] cells = result.rawCells();
        //遍历
        for (Cell cell : cells) {
            //获取所有的朋友
            final String column = Bytes.toString(CellUtil.cloneQualifier(cell));
            final String value = Bytes.toString(CellUtil.cloneValue(cell));
            //去朋友的朋友关系中 删除 自己
            final Delete delete1 = new Delete(Bytes.toBytes(column));
            delete1.addColumns(Bytes.toBytes("friend"), Bytes.toBytes(uid));
            deletes.add(delete1);
        }
        System.out.println(deletes.size());
        user.delete(deletes);
        System.out.println("删除" + uid + "成功");
    }

    //释放连接
    @After
    public void realse() {
        if (conn != null) {
            try {
                conn.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
